package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SimUserLevelBo;
import org.dromara.system.domain.bo.VariableLevelBo;
import org.dromara.system.domain.vo.SimUserLevelVo;
import org.dromara.system.service.ISimUserLevelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户等级
 *
 * @author Lion Li
 * @date 2025-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userLevel")
public class SimUserLevelController extends BaseController {

    private final ISimUserLevelService simUserLevelService;

    /**
     * 查询用户等级列表
     */
//    @SaCheckPermission("system:userLevel:list")
    @GetMapping("/list")
    public TableDataInfo<SimUserLevelVo> list(SimUserLevelBo bo, PageQuery pageQuery) {
        return simUserLevelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户等级列表
     */
//    @SaCheckPermission("system:userLevel:export")
    @Log(title = "用户等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SimUserLevelBo bo, HttpServletResponse response) {
        List<SimUserLevelVo> list = simUserLevelService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户等级", SimUserLevelVo.class, response);
    }

    /**
     * 获取用户等级详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userLevel:query")
    @GetMapping("/{id}")
    public R<SimUserLevelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(simUserLevelService.queryById(id));
    }

    /**
     * 新增用户等级
     */
//    @SaCheckPermission("system:userLevel:add")
    @Log(title = "用户等级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SimUserLevelBo bo) {
        return toAjax(simUserLevelService.insertByBo(bo));
    }

    /**
     * 修改用户等级
     */
//    @SaCheckPermission("system:userLevel:edit")
    @Log(title = "用户等级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SimUserLevelBo bo) {
        return toAjax(simUserLevelService.updateByBo(bo));
    }

    /**
     * 删除用户等级
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userLevel:remove")
    @Log(title = "用户等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simUserLevelService.deleteWithValidByIds(List.of(ids), true));
    }


    @PostMapping("/queryVariableLevel")
    public R<List<SimUserLevelVo>> queryVariableLevel(@Validated @RequestBody VariableLevelBo bo) {
        return R.ok(simUserLevelService.queryVariableLevel(bo));
    }

}
